package com.fc.v2.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.fc.v2.mapper.auto.ApplyProjectMapper;
import com.fc.v2.mapper.auto.TsysUserMapper;
import com.fc.v2.model.Examine;
import com.fc.v2.model.auto.ApplyProject;
import com.fc.v2.shiro.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.v2.common.base.BaseService;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.mapper.auto.ApplyAlterMapper;
import com.fc.v2.model.auto.ApplyAlter;
import com.fc.v2.model.auto.ApplyAlterExample;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.util.SnowflakeIdWorker;
import com.fc.v2.util.StringUtils;

/**
 * 项目变更表 ApplyAlterService
 * @Title: ApplyAlterService.java 
 * @Package com.fc.v2.service 
 * @author Archie_自动生成
 * @email ${email}
 * @date 2021-11-25 14:47:49  
 **/
@Service
public class ApplyAlterService implements BaseService<ApplyAlter, ApplyAlterExample>{
	@Autowired
	private ApplyAlterMapper applyAlterMapper;

	@Autowired
	private TsysUserMapper tsysUserMapper;

	@Autowired
	private ApplyProjectMapper applyProjectMapper;

	/**
	 * 申报人分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<ApplyAlter> list(Tablepar tablepar,ApplyAlter applyAlter){
		ApplyAlterExample testExample=new ApplyAlterExample();
		//当前用户
		String userId = ShiroUtils.getUserId();
		//搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
			testExample.createCriteria().andProNameLike(tablepar.getSearchText()).andManIdEqualTo(userId);
		}else {//大搜索
			testExample.createCriteria().andManIdEqualTo(userId);
		}
		//表格排序
		//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
		//	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		//}else{
		//	testExample.setOrderByClause("id ASC");
		//}
		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<ApplyAlter> list= applyAlterMapper.selectByExample(testExample);
		PageInfo<ApplyAlter> pageInfo = new PageInfo<ApplyAlter>(list);
		return  pageInfo;
	}

	/**
	 * 单位分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<ApplyAlter> listDanwei(Tablepar tablepar,ApplyAlter applyAlter){
	        ApplyAlterExample testExample=new ApplyAlterExample();
	        //当前用户
		 	String userId = ShiroUtils.getUserId();
			 //通过当前用户id查找所在单位名称
			 String deptName = tsysUserMapper.selectByPrimaryKey(userId).getDeptName();
			 //单位审核状态
			 List<Integer> stateList = new ArrayList<>();
			 stateList.add(Examine.NEED_EXAMINE.code);
			 stateList.add(Examine.PASSED.code);
			 stateList.add(Examine.BANNED.code);
			//搜索
			if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
	        	testExample.createCriteria().andProNameLike(tablepar.getSearchText()).andManDeptEqualTo(deptName).andDeptPassIn(stateList);
	        }else {//大搜索
	        	testExample.createCriteria().andManDeptEqualTo(deptName).andDeptPassIn(stateList);
	        }
			//表格排序
			//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        //}else{
	        //	testExample.setOrderByClause("id ASC");
	        //}
	        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
	        List<ApplyAlter> list= applyAlterMapper.selectByExample(testExample);
	        PageInfo<ApplyAlter> pageInfo = new PageInfo<ApplyAlter>(list);
	        return  pageInfo;
	 }

	/**
	 * 社科联分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<ApplyAlter> listSkl(Tablepar tablepar,ApplyAlter applyAlter){
		ApplyAlterExample testExample=new ApplyAlterExample();
		//当前用户
		String userId = ShiroUtils.getUserId();

		//社科联审核状态：待审核、通过、不通过
		List<Integer> stateList = new ArrayList<>();
		stateList.add(Examine.NEED_EXAMINE.code);
		stateList.add(Examine.PASSED.code);
		stateList.add(Examine.BANNED.code);
		//搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
			testExample.createCriteria().andProNameLike(tablepar.getSearchText()).andSklPassIn(stateList);
		}else {//大搜索
			testExample.createCriteria().andSklPassIn(stateList);
		}
		//表格排序
		//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
		//	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		//}else{
		//	testExample.setOrderByClause("id ASC");
		//}
		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<ApplyAlter> list= applyAlterMapper.selectByExample(testExample);
		PageInfo<ApplyAlter> pageInfo = new PageInfo<ApplyAlter>(list);
		return  pageInfo;
	}

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=ConvertUtil.toListStrArray(ids);
			ApplyAlterExample example=new ApplyAlterExample();
			example.createCriteria().andIdIn(lista);
			return applyAlterMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public ApplyAlter selectByPrimaryKey(String id) {
				
			return applyAlterMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(ApplyAlter record) {
		//项目状态为待审核
		record.setState(Examine.NEED_EXAMINE.code);
		//单位状态为待审核
		record.setDeptPass(Examine.NEED_EXAMINE.code);
		//添加时间
		record.setUpdateTime(new Date());
		return applyAlterMapper.updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKeySelectiveDanwei(ApplyAlter record) {

	 	//添加时间
		record.setDeptUpdateTime(new Date());


		return applyAlterMapper.updateByPrimaryKeySelective(record);
	}


	public int updateByPrimaryKeySelectiveSkl(ApplyAlter record) {

		//添加时间
		record.setSklUpdateTime(new Date());

		//审核通过，初试化立项项目状态
		if(record.getSklPass() == Examine.PASSED.code){
			ApplyProject applyProject = new ApplyProject();
			applyProject = applyProjectMapper.selectByPrimaryKey(record.getProId());
			applyProject.setProState(Examine.NEED_SUBMIT.code);
			//设置数字7，使得不在单位和社科联的审核状态，无法被查出
			applyProject.setDeptPass(7);
			applyProject.setSklPass(7);
			applyProjectMapper.updateByPrimaryKeySelective(applyProject);
		}
		return applyAlterMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(ApplyAlter record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		//添加项目负责人id
		record.setManId(ShiroUtils.getUserId());
		//添加时间
		record.setUpdateTime(new Date());
		//单位审核状态为待审核
		record.setDeptPass(Examine.NEED_EXAMINE.code);
		//项目变更状态为待审核
		record.setState(Examine.NEED_EXAMINE.code);
			
				
		return applyAlterMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(ApplyAlter record, ApplyAlterExample example) {
		
		return applyAlterMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(ApplyAlter record, ApplyAlterExample example) {
		
		return applyAlterMapper.updateByExample(record, example);
	}

	@Override
	public List<ApplyAlter> selectByExample(ApplyAlterExample example) {
		
		return applyAlterMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(ApplyAlterExample example) {
		
		return applyAlterMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(ApplyAlterExample example) {
		
		return applyAlterMapper.deleteByExample(example);
	}
	
	/**
	 * 修改权限状态展示或者不展示
	 * @param applyAlter
	 * @return
	 */
	public int updateVisible(ApplyAlter applyAlter) {
		return applyAlterMapper.updateByPrimaryKeySelective(applyAlter);
	}


}
